<template>
    <div class="container_box">
        <el-scrollbar ref="scrollbarRef" class="main_box2">
            <el-card class="top_step" shadow="never">
                <Step ref="StepRef" :total-steps="totalSteps" :steps-label="stepsLabel" :current-step="currentStep" />
            </el-card>
            <step1 v-show="currentStep == 1" ref="step1Ref" />
            <step2 v-show="currentStep == 2" ref="step2Ref" />
            <step3 v-show="currentStep == 3" ref="step3Ref" :is-clues="isClues" />
        </el-scrollbar>
        <div class="footer_box">
            <el-button v-if="currentStep == 1" @click="cancel">取消</el-button>
            <el-button v-else plain @click="preStep">上一步</el-button>
            <el-button v-if="currentStep == 3" type="primary" @click="save">完成</el-button>
            <el-button v-else type="primary" @click="nextStep">下一步</el-button>
        </div>
    </div>
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted, nextTick } from 'vue';
import { ElMessage } from 'element-plus';
import router from '@/router';
import step1 from './step1.vue';
import step2 from './step2.vue';
import step3 from './step3.vue';
import Step from '@/components/Step.vue';
import { IsNotEmpty, getSetionId } from '@/utils/utils';
import { taskUpdate, createTask, taskDetail } from '@/api/employe/employeetasks/taskmanage';
const StepRef = ref(null);
const step1Ref = ref(null);
const step2Ref = ref(null);
const step3Ref = ref(null);
const taskId = ref(0);
const task_type = ref(1);
const isEdit = ref(false);
const isClues = ref(false); //是否是线索
const stepsLabel = reactive([
    { stepIndex: 1, title: '基础设置' },
    { stepIndex: 2, title: '任务分配机制' },
    { stepIndex: 3, title: '任务指标设定' }
]);
const totalSteps = ref(3);
const currentStep = ref(1);

const scrollbarRef = ref<any>(null);
/**
 * @description: 滚动到顶部
 */
const setScrollTopHandle = () => {
    nextTick(() => {
        scrollbarRef.value?.setScrollTop(0);
    });
};

//取消
const cancel = () => {
    router.back();
};
// 上一步
const preStep = () => {
    currentStep.value -= 1;
    StepRef.value.back();
    if (currentStep.value <= 1) currentStep.value = 1;
    setScrollTopHandle();
};
//下一步
const nextStep = async () => {
    if (currentStep.value == 1) {
        await step1Ref.value.save();
        if (!step1Ref.value.step1) return;
    }
    if (currentStep.value == 2) {
        await step2Ref.value.save();
        isClues.value = step2Ref.value.step2Data.object_type == 1;
        if (!step2Ref.value.step2) return;
    }

    StepRef.value.next();
    currentStep.value += 1;
    if (currentStep.value >= 3) currentStep.value = 3;
    setScrollTopHandle();
};
// 任务详情
const getTaskDetail = async () => {
    const res: any = await taskDetail({ id: taskId.value });
    step1Ref.value.form.name = res.name;
    step1Ref.value.form.time = res.time;
    step1Ref.value.form.type = res.type;
    step1Ref.value.form.desc = res.desc;
    // ++++++++第二步回显数据+++++++
    step2Ref.value.step2Data.work_user_type = res.work_user_type;
    if (res.work_user_type == 2) {
        step2Ref.value.step2Data.work_user_ids = getSetionId('id', res.work_user_ids); //
        step2Ref.value.creators = res.work_user_ids; // 指定员工Id
    }
    step2Ref.value.step2Data.execution_mode = res.execution_mode;
    step2Ref.value.step2Data.object_type = res.object_type;
    step2Ref.value.step2Data.object_source = res.object_source;
    // isClues.value = res.object_type == 1 //线索
    // 选择
    if (res.object_type == 2 && res.object_source == 1) {
        step2Ref.value.step2Data.cust_number = res.object_ids.length;
        step2Ref.value.step2Data.object_ids = getSetionId('id', res.object_ids);
    } else if (res.object_type == 2 && res.object_source == 2) {
        // 导入
        step2Ref.value.step2Data.import_file = res.import_file;
        step2Ref.value.step2Data.cust_number = res.import_num;
        step2Ref.value.step2Data.import_id = res.import_id;
        step2Ref.value.fileInfo.name = res.import_file;
    }
    if (res.object_type == 1 && res.object_source == 1) {
        step2Ref.value.step2Data.clue_number = res.object_ids.length;
        step2Ref.value.step2Data.creators3 = res.object_ids.length;
        step2Ref.value.step2Data.object_ids = getSetionId('id', res.object_ids);
    } else if (res.object_type == 1 && res.object_source == 2) {
        step2Ref.value.step2Data.import_file = res.import_file;
        step2Ref.value.step2Data.clue_number = res.import_num;
        step2Ref.value.step2Data.import_id = res.import_id;
        step2Ref.value.fileInfo.name = res.import_file;
    }
    step2Ref.value.step2Data.cycle_rule = res.cycle_rule ? res.cycle_rule : {};
    if (res.execution_mode == 2) {
        step2Ref.value.step2Data.entry_rule = res.entry_rule;
    }
    if (res.entry_rule.entry_day_switch == 1) {
        step2Ref.value.step2Data.checkList.push('entry_day_switch');
    }
    if (res.entry_rule.entry_user_switch == 1) {
        step2Ref.value.step2Data.checkList.push('entry_user_switch');
    }
    if (res.entry_rule.entry_day_switch == 1 && res.entry_rule.entry_user_switch == 1) {
        step2Ref.value.step2Data.checkList = ['entry_day_switch', 'entry_user_switch'];
    }
    // ++++++++第三步回显数据+++++++
    step3Ref.value.step3Data.remark = res.remark;
    step3Ref.value.step3Data.call_deploy = res.call_deploy; //
    step3Ref.value.step3Data.visit_deploy = res.visit_deploy;
    if (res.visit_deploy.signin_range_switch == 1) {
        step3Ref.value.step3Data.checkList.push('signin_range_switch');
    }
    if (res.visit_deploy.signin_cert_switch == 1) {
        step3Ref.value.step3Data.checkList.push('signin_cert_switch');
    }
    if (res.visit_deploy.signin_range_switch == 1 && res.visit_deploy.signin_cert_switch == 1) {
        step3Ref.value.step3Data.checkList = ['signin_range_switch', 'signin_cert_switch'];
    }
    step3Ref.value.step3Data.join_deploy = res.join_deploy;
    step3Ref.value.step3Data.grade_switch = res.grade_switch;
    step3Ref.value.step3Data.auto_switch = res.auto_switch;
};
// 保存、编辑
const save = async () => {
    if (isEdit.value == 'look') {
        router.back();
        return;
    } else {
        await step3Ref.value.save();
        if (!step3Ref.value.step3) return; //第三部验证
    }
    const params = {
        ...step1Ref.value.form
    };
    const step2FormData = step2Ref.value.step2Data;
    const step3FormData = step3Ref.value.step3Data;
    //————————————————————————第二步——————————————————————————————————————————————————
    //指定员工
    params.work_user_type = step2FormData.work_user_type;
    if (step2FormData.work_user_type == 2) {
        params.work_user_ids = step2FormData.work_user_ids;
    }
    // 录入类型 /对象类型
    if (task_type.value !== 3) {
        params.object_type = step2FormData.object_type;
    }
    // 选择外呼对象
    params.execution_mode = step2FormData.execution_mode;
    if (step2FormData.execution_mode == 1 || task_type.value == 3) {
        params.object_source = step2FormData.object_source;
        //选择客户
        if (step2FormData.object_source == 1) {
            params.object_ids = step2FormData.object_ids;
        } else {
            params.import_id = step2FormData.import_id;
        }
        if (step2FormData.execution_mode == 2) {
            params.entry_rule = step2FormData.entry_rule;
        }
        // // 分配规则
        params.cycle_rule = {
            assign_type: step2FormData.cycle_rule.assign_type
        };
        // params.cycle_rule.assign_type = step2FormData.cycle_rule.assign_type
        if (step2FormData.cycle_rule.assign_type == 2) {
            params.cycle_rule.cycle_type = step2FormData.cycle_rule.cycle_type;
            params.cycle_rule.cycle_unit = step2FormData.cycle_rule.cycle_unit;
            params.cycle_rule.cycle_time = step2FormData.cycle_rule.cycle_time;
            params.cycle_rule.cycle_num = step2FormData.cycle_rule.cycle_num;
            params.cycle_rule.cycle_date = step2FormData.cycle_rule.cycle_date;
        }
        //分配方式
        params.cycle_rule.assign_mode = step2FormData.cycle_rule.assign_mode;

        // params.cycle_rule = step2FormData.cycle_rule
    } else {
        //员工录入
        params.entry_rule = step2FormData.entry_rule;
    }
    //————————————————————————第三步————————————————————————
    params.remark = step3FormData.remark;
    // 电话销售
    if (task_type.value == 1) {
        // 电话拨打
        params.call_deploy = {
            type: step3FormData.call_deploy.type
        };
        // 外呼结果 网络电话外呼
        if (params.call_deploy.type == 1) {
            params.call_deploy.result_connect_num = step3FormData.call_deploy.result_connect_num;
            params.call_deploy.result_unconnected_num = step3FormData.call_deploy.result_unconnected_num;
            params.call_deploy.result_refuse_num = step3FormData.call_deploy.result_refuse_num;
        }
        // 上传凭证
        params.call_deploy.cert_switch = step3FormData.call_deploy.cert_switch;
        params.call_deploy.cert_num = step3FormData.call_deploy.cert_num;
    } else if (task_type.value == 2) {
        // 客户拜访
        params.visit_deploy = step3FormData.visit_deploy;
        // // 拜访计划
        // params.visit_deploy={
        //   switch: step3FormData.visit_deploy.switch
        // }
        // // params.visit_deploy.switch = step3FormData.visit_deploy.switch
        // if(step3FormData.visit_deploy.switch==1){
        //   params.visit_deploy.switch = step3FormData.visit_deploy.switch
        //   //拜访签到
        //   params.visit_deploy.signin_switch = step3FormData.visit_deploy.signin_switch
        //   if(step3FormData.visit_deploy.signin_switch==1){
        //     params.visit_deploy.signin_range_switch = step3FormData.visit_deploy.signin_range_switch
        //     params.visit_deploy.signin_range_distance = step3FormData.visit_deploy.signin_range_distance
        //     params.visit_deploy.signin_cert_switch = step3FormData.visit_deploy.signin_cert_switch
        //     params.visit_deploy.signin_cert_num = step3FormData.visit_deploy.signin_cert_num
        //   }
        // }
        // //上传凭证
        // params.visit_deploy.cert_switch = step3FormData.visit_deploy.cert_switch
        // if(step3FormData.visit_deploy.cert_switch==1)params.visit_deploy.cert_num = step3FormData.visit_deploy.cert_num
        // //跟进记录
        // params.visit_deploy.follow_switch = step3FormData.visit_deploy.follow_switch
        // if(step3FormData.visit_deploy.follow_switch==1)params.visit_deploy.follow_num = step3FormData.visit_deploy.follow_num
    } else {
        //企微招募
        params.join_deploy = step3FormData.join_deploy;
    }
    //客户评级
    params.grade_switch = step3FormData.grade_switch;
    //任务状态
    params.auto_switch = step3FormData.auto_switch;
    //编辑
    isEdit.value == 'edit' ? (params.id = taskId.value) : '';
    const res: any = await createTask(params);
    ElMessage.success(
        `${isEdit.value ? '编辑' : '创建'}${['电话销售', '客户拜访', '企微招募'][task_type.value - 1]}成功！`
    );
    router.push('/employee/employeetasks/taskmanage');
};
onMounted(() => {
    const type = router.currentRoute.value.query?.val;
    task_type.value = router.currentRoute.value.query?.val;
    taskId.value = router.currentRoute.value.query?.id;
    isEdit.value = router.currentRoute.value.query?.cur;
    if (taskId.value) {
        getTaskDetail();
    }
    if (isEdit.value == 'look' && IsNotEmpty(taskId.value)) {
        step1Ref.value.step1Diabled = true;
        step2Ref.value.step2Diabled = true;
        step3Ref.value.step3Diabled = true;
    }
});
</script>
<style scoped lang="scss">
.top_step {
    margin-bottom: 15px;
    border-top: none;
}
</style>
